<?php

namespace app\queue\stomp;

use app\queue\BaseConsumer;
use common\service\TaskAssembleService;
use think\Exception;
use Webman\Stomp\RetryAbleConsumer;
/**
 * 任务就绪检查
 */
class TaskAssembleCheckConsumer extends BaseConsumer implements RetryAbleConsumer
{
    // 要消费的队列名ases
    public $queue = 'task_assemble_check';

    // 连接名，对应 config/redis_queue.php 里的连接`
    public $connection = 'default';

    /**
     * 消费
     * @param $data
     * @return void
     * @throws Exception
     */
    public function doConsume($data)
    {
        $taskAssembleService = TaskAssembleService::getInstance();
        $res = $taskAssembleService->checkTaskCanAssemble($data['task_id']);
        APP_DEBUG && trace(var_export($data['task_id'], true), 'DEBUG');
        APP_DEBUG && trace(var_export($res, true), 'DEBUG');
    }

    /**
     * 重试间隔
     * @return int
     */
    public function getTtr()
    {
        return 20;
    }

    /**
     * 能否重试
     * @param $attempt
     * @param $error
     * @return bool
     */
    public function canRetry($attempt, $error)
    {
        trace(format_exception_info($error), 'ERR');
        return $attempt <= 3;
    }
}
